﻿using StockPredictor.App_Start;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace StockPredictor.Models
{
    public class UserPreferencesRecordsContext : DbContext
    {
        public DbSet<UserPreferencesRecordModel> UserPreferencesRecords { get; set; }

        public UserPreferencesRecordsContext()
            : base("DefaultConnection")
        {
        }
    }

    [Table("UserPreferences")]
    public class UserPreferencesRecordModel
    {
        /// <summary>
        /// UserPreferences Record ID
        /// </summary>
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int PreferenceId { get; set; }

        /// <summary>
        /// User Id for the UserPreferences record
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// PeRatioPrcntg for this UserPreferences record
        /// </summary>
        [Range(1, 100)]
        public int PeRatioPrcntg { get; set; }

        /// <summary>
        /// EpsPrcntg for this UserPreferences record
        /// </summary> 
        [Range(1, 100)]
        public int EpsPrcntg { get; set; }

        /// <summary>
        /// NotificationFrequency for this UserPreferences record
        /// </summary>
        public string NotificationFrequency { get; set; }

        /// <summary>
        /// Notification content for this UserPreferences record
        /// </summary>
        public string NotificationSendStocksToSell { get; set; }

        /// <summary>
        /// Notification content for this UserPreferences record
        /// </summary>
        public string NotificationSendStocksToBuy { get; set; }

        /// <summary>
        /// Last time notification was sent
        /// </summary>
        public DateTime NotificationLastSentTime { get; set; }
    }

    public static class UserPreferencesUtilities
    {
        public static int userPreferencesExist;
        public static int userPreferencePeRatio;
        public static int userPreferenceEpsPrcntg;
        public static int userPreferenceSpreadPrcntg;

        public static List<UserPreferencesRecordModel> GetUserPreferencesRecordsForUser(string userName)
        {
            UserPreferencesRecordsContext UserPreferencesContext = new UserPreferencesRecordsContext();
            int userId = AccountUtilities.GetUserIdForUser(userName);
            var records = UserPreferencesContext.UserPreferencesRecords.Where(stock => stock.UserId == userId).ToList();

            if (records.Count() == 0)
                userPreferencesExist = 0;
            else
            {
                userPreferencesExist = 1;
                foreach (var record in records)
                {
                    userPreferencePeRatio = record.PeRatioPrcntg;                   
                    userPreferenceEpsPrcntg = record.EpsPrcntg;
                }

            }
            return records;
        }
    }
}